package com.flexcloudsdk.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import com.flexcloudsdk.environment.CloudEnvironment;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CloudEnvironmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(CloudEnvironmentServlet.class.getName());
	public static final String allCall = "all";
	public static final String versionCall = "version"; 
	public static final String appVersionCall = "appVersion";
	public static final String appIdCall = "appId";
	public static final String isProductionCall = "isProduction";
	public static final String isDevelopmentCall = "isDevelopment";
	public static final String fileSeparatorCall = "fileSeparator";
	public static final String pathSeparatorCall = "pathSeparator";
	public static final String lineSeparatorCall = "lineSeparator";
	public static final String javaVersionCall = "javaVersion";
	public static final String javaSpecVersionCall = "javaSpecVersion";
	public static final String javaSpecNameCall = "javaSpecName";
	public static final String javaVMVendorCall = "javaVMVendor";
	public static final String javaVMNameCall = "javaVMName";
	public static final String javaVMSpecVersionCall = "javaVMSpecVersion";
	public static final String javaVMSpecVendorCall = "javaVMSpecVendor";
	public static final String javaVMSpecNameCall = "javaVMSpecName";
	public static final String userDirCall = "userDir";
	
	private CloudEnvironment enviornment = new CloudEnvironment();
	@SuppressWarnings("unused")
	private String method;
	private String sourceMethod;
	private String sourceClass;

	
	public CloudEnvironmentServlet() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
	    sourceClass = CloudEnvironmentServlet.class.getName();
		log.info("*****CloudEnvironmentServlet constructor********");
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException 
	{
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
	    resp.setContentType("text/xml");
		PrintWriter writer = resp.getWriter();
		writer.println("<?xml version=\"1.0\"?>");
		writer.println("<root>");

		method = req.getMethod();
		String operation = req.getParameter("operation");
		if (operation == null) {
			handleNullOperation(writer, req);
		} else if (operation.isEmpty()) {
			handleEmptyOperation(writer, req);
		} else	if (operation.equalsIgnoreCase(allCall)) {
			all(writer, req, resp);
		} else 	if (operation.equalsIgnoreCase(versionCall))
		{
			version(writer, req, resp);
		} else if (operation.equalsIgnoreCase(appVersionCall)) 
		{
			appVersion(writer, req, resp);
		} else if (operation.equalsIgnoreCase(isProductionCall))
		{
			isProduction(writer);
		} else if (operation.equalsIgnoreCase(isDevelopmentCall))
		{
			isDevelopment(writer);
		} else if (operation.equalsIgnoreCase(fileSeparatorCall))
		{
			fileSeparator(writer, req, resp);
		} else if (operation.equalsIgnoreCase(lineSeparatorCall))
		{
			lineSeparator(writer, resp);
		} else 	if (operation.equalsIgnoreCase(javaVersionCall)) 
		{
			javaVersion(writer, resp);
		} else 	if (operation.equalsIgnoreCase(javaSpecVersionCall)) 
		{
			javaSpecVersion(writer, resp);
		} else if (operation.equalsIgnoreCase(javaSpecNameCall)) 
		{
			javaSpecName(writer, resp);
		} else if (operation.equalsIgnoreCase(javaVMVendorCall)) 
		{
			javaVMVendor(writer, resp);
		} else if (operation.equalsIgnoreCase(javaVMNameCall)) 
		{
			javaVMName(writer, resp);
		} else if (operation.equalsIgnoreCase(javaVMSpecVersionCall)) 
		{
			javaVMSpecVersion(writer, resp);
		} else if (operation.equalsIgnoreCase(javaVMSpecVendorCall)) 
		{
			javaVMSpecVendor(writer, resp);
		} else if (operation.equalsIgnoreCase(javaVMSpecNameCall)) 
		{
			javaVMSpecName(writer, resp);
		} else if (operation.equalsIgnoreCase(userDirCall)) 
		{
			userDir(writer, resp);
		}
		writer.println("</root>");
	}

	private void userDir(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineuserDir = enviornment.userDir();
		if ((engineuserDir != null) && (!engineuserDir.isEmpty())) {
			writer.println("<result>" + engineuserDir + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + userDirCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void javaVMSpecName(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineJavaVMSpecName = enviornment.javaSpecName();
		if ((engineJavaVMSpecName != null) && (!engineJavaVMSpecName.isEmpty())) {
			writer.println("<result>" + engineJavaVMSpecName + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + javaVMSpecNameCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void javaVMSpecVendor(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineJavaVMSpecVendor = enviornment.javaSpecVendor();
		if ((engineJavaVMSpecVendor != null) && (!engineJavaVMSpecVendor.isEmpty())) {
			writer.println("<result>" + engineJavaVMSpecVendor + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + javaVMSpecVendorCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void javaVMSpecVersion(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		String engineJavaVMSpecVersion = enviornment.javaVMSpecVersion();
		if ((engineJavaVMSpecVersion != null) && (!engineJavaVMSpecVersion.isEmpty())) {
			writer.println("<result>" + engineJavaVMSpecVersion + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + javaVMSpecVersionCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void javaVMName(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineJavaVMName = enviornment.javaVMName();
		if ((engineJavaVMName != null) && (!engineJavaVMName.isEmpty())) {
			writer.println("<result>" + engineJavaVMName + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + javaVMNameCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void javaVMVendor(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineJavaVMVendor = enviornment.javaVMVendor();
		if ((engineJavaVMVendor != null) && (!engineJavaVMVendor.isEmpty())) {
			writer.println("<result>" + engineJavaVMVendor + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + javaVMVendorCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void javaSpecName(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineJavaSpecName = enviornment.javaSpecName();
		if ((engineJavaSpecName != null) && (!engineJavaSpecName.isEmpty())) {
			writer.println("<result>" + engineJavaSpecName + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + javaSpecNameCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void javaSpecVersion(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineJavaSpecVersion = enviornment.javaSpecVersion();
		if ((engineJavaSpecVersion != null) && (!engineJavaSpecVersion.isEmpty())) {
			writer.println("<result>" + engineJavaSpecVersion + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + javaSpecVersionCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void javaVersion(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineJavaVersion = enviornment.javaVersion();
		if ((engineJavaVersion != null) && (!engineJavaVersion.isEmpty())) {
			writer.println("<result>" + engineJavaVersion + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + javaVersionCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void lineSeparator(PrintWriter writer, HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineLineSeparator = enviornment.lineSeparator();
		if ((engineLineSeparator != null) && (!engineLineSeparator.isEmpty())) {
			writer.println("<result>" + engineLineSeparator + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + lineSeparatorCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void fileSeparator(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineFileSeparator = enviornment.fileSeparator();
		if ((engineFileSeparator != null) && (!engineFileSeparator.isEmpty())) {
			writer.println("<result>" + engineFileSeparator + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + fileSeparatorCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void isDevelopment(PrintWriter writer) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		Boolean engineIsDevelopment = enviornment.isDevelopment();
		writer.println("<result>" + engineIsDevelopment + "</result>");
	}

	private void isProduction(PrintWriter writer) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		Boolean engineIsProduction  = enviornment.isProduction();
		writer.println("<result>" + engineIsProduction + "</result>");
	}

	private void appVersion(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineAppVersion  = enviornment.appVerion();
		if ((engineAppVersion != null) && (!engineAppVersion.isEmpty())) {
			writer.println("<result>" + engineAppVersion + "</result>");
		}
		else {
			try {
				resp.sendError(400,"<result>" + appVersionCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void all(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineAll            = enviornment.toString();
		if ((engineAll != null) && (!engineAll.isEmpty())) {
			writer.println("<result>" + engineAll + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + allCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void version(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String engineVersion = enviornment.version();
		if ((engineVersion != null) && (!engineVersion.isEmpty())) {
			writer.println("<result>" + engineVersion + "</result>");
		}
		else {
			try {
				resp.sendError(400, "<result>" + versionCall + " call failed" + "</result>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	
	private void handleNullOperation(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		log.warning("operation is null");

		writer.println("<result>No operation found while calling "
				+ req.getServerName() + "servlet</result>");
	}

	private void handleEmptyOperation(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		log.warning("operation is empty");
		writer.println("<result>Must pass an operation in calling "
				+ req.getServerName() + "servlet</result>");
	}
	
}
